#include <iostream>
#include <memory>
#include <string>

using std::cout;
using std::endl;
using std::unique_ptr;
using std::shared_ptr;
using std::string;

struct FILEClose
{
    void operator()(FILE* ptr) const
    {
        /* delete ptr; */
        if(ptr)
        {
            fclose(ptr);
            cout << "fclose(ptr)" << endl;
        }
    }
};
void test()
{
    /* unique_ptr<int> up(new int(10)); */
    string msg = "hello,world\n";
    unique_ptr<FILE, FILEClose> up(fopen("wd.txt", "a+"));
    //可以通过调用get获取智能指针关联的裸指针
    fwrite(msg.c_str(), 1, msg.size(), up.get());
    /* fclose(up.get()); */
}

void test2()
{
    string msg = "welcome1\n";
    FILEClose fc;//fc是左值
    /* shared_ptr<FILE> sp(fopen("test.txt", "a+"), FILEClose()); */
    shared_ptr<FILE> sp(fopen("test.txt", "a+"), fc);
    //可以通过调用get获取智能指针关联的裸指针
    fwrite(msg.c_str(), 1, msg.size(), sp.get());
    /* fclose(sp.get()); */
}
int main(int argc, char *argv[])
{
    test2();
    return 0;
}

